package cn.chiship.framework.upms.biz.system.controller;


import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import cn.chiship.framework.upms.biz.system.entity.UpmsCategoryDict;
import cn.chiship.framework.upms.biz.system.entity.UpmsCategoryDictExample;
import cn.chiship.framework.upms.biz.system.pojo.dto.UpmsCategoryDictDto;
import cn.chiship.framework.upms.biz.system.service.UpmsCategoryDictService;
import cn.chiship.sdk.core.annotation.Authorization;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.base.BaseController;
import cn.chiship.sdk.framework.base.BaseService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 分类字典控制层
 * 2022/4/1
 *
 * @author lijian
 */
@RestController
@RequestMapping("/upmsCategoryDict")
@Api(tags = "分类字典")
public class UpmsCategoryDictController extends BaseController<UpmsCategoryDict, UpmsCategoryDictExample> {

    @Resource
    private UpmsCategoryDictService upmsCategoryDictService;

    @Override
    public BaseService getService() {
        return upmsCategoryDictService;
    }

    @SystemOptionAnnotation(describe = "根据类型查找")
    @ApiOperation(value = "根据类型查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "所属类型", dataTypeClass = String.class, paramType = "query"),

    })
    @GetMapping(value = "/treeByType")
    public ResponseEntity<BaseResult> treeByType(
            @RequestParam(required = false, defaultValue = "", value = "type") String type) {
        UpmsCategoryDictExample upmsCategoryDictExample = new UpmsCategoryDictExample();
        /**
         * 创造条件
         */
        UpmsCategoryDictExample.Criteria upmsCategoryDictCriteria = upmsCategoryDictExample.createCriteria();
        upmsCategoryDictCriteria.andIsDeletedEqualTo(BaseConstants.NO);
        if (!StringUtil.isNullOrEmpty(type)) {
            upmsCategoryDictCriteria.andTypeEqualTo(type);
        }
        upmsCategoryDictExample.setOrderByClause("orders desc");
        List<UpmsCategoryDict> upmsCategoryDicts = upmsCategoryDictService.selectByExample(upmsCategoryDictExample);
        return super.responseEntity(BaseResult.ok(assemblyCategoryDictTree("0", upmsCategoryDicts)));
    }

    @ApiOperation(value = "根据类型查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "所属类型", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "pid", value = "所属父级", dataTypeClass = String.class, paramType = "query"),
    })
    @GetMapping(value = "/listByTypeAndPid")
    public ResponseEntity<BaseResult> listByTypeAndPid(
            @RequestParam(required = false, defaultValue = "", value = "type") String type,
            @RequestParam(required = false, defaultValue = "", value = "pid") String pid) {
        UpmsCategoryDictExample upmsCategoryDictExample = new UpmsCategoryDictExample();
        /**
         * 创造条件
         */
        UpmsCategoryDictExample.Criteria upmsCategoryDictCriteria = upmsCategoryDictExample.createCriteria();
        upmsCategoryDictCriteria.andIsDeletedEqualTo(BaseConstants.NO);
        if (!StringUtil.isNullOrEmpty(type)) {
            upmsCategoryDictCriteria.andTypeEqualTo(type);
        }
        if (!StringUtil.isNullOrEmpty(pid)) {
            upmsCategoryDictCriteria.andPidEqualTo(pid);
        }
        upmsCategoryDictExample.setOrderByClause("orders desc");
        List<UpmsCategoryDict> upmsCategoryDicts = upmsCategoryDictService.selectByExample(upmsCategoryDictExample);
        return super.responseEntity(BaseResult.ok(upmsCategoryDicts));
    }

    private List<JSONObject> assemblyCategoryDictTree(String pid, List<UpmsCategoryDict> upmsCategoryDicts) {
        List<JSONObject> treeVos = new ArrayList<>();
        for (UpmsCategoryDict categoryDict : upmsCategoryDicts) {
            if (pid.equals(categoryDict.getPid())) {
                treeVos.add(JSON.parseObject(JSON.toJSONString(categoryDict)));
            }
        }
        for (int i = 0; i < treeVos.size(); i++) {
            List<JSONObject> children = assemblyCategoryDictTree(treeVos.get(i).getString("id"), upmsCategoryDicts);
            if (!children.isEmpty()) {
                treeVos.get(i).put("children", children);
            }
        }
        return treeVos;
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_SAVE, describe = "保存分类字典")
    @ApiOperation(value = "保存分类字典")
    @PostMapping(value = "save")
    @Authorization
    public ResponseEntity<BaseResult> save(@RequestBody @Valid UpmsCategoryDictDto upmsCategoryDictDto) {
        UpmsCategoryDict upmsCategoryDict = new UpmsCategoryDict();
        BeanUtils.copyProperties(upmsCategoryDictDto, upmsCategoryDict);
        return super.responseEntity(super.save(upmsCategoryDict));
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_UPDATE, describe = "更新分类字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = String.class, paramType = "path"),
    })
    @ApiOperation(value = "更新分类字典")
    @PostMapping(value = "update/{id}")
    @Authorization
    public ResponseEntity<BaseResult> update(@PathVariable("id") String id, @RequestBody @Valid UpmsCategoryDictDto upmsCategoryDictDto) {
        UpmsCategoryDict upmsCategoryDict = new UpmsCategoryDict();
        BeanUtils.copyProperties(upmsCategoryDictDto, upmsCategoryDict);
        return super.responseEntity(super.update(id, upmsCategoryDict));
    }

    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_REMOVE, describe = "删除分类字典")
    @ApiOperation(value = "删除分类字典")
    @PostMapping(value = "remove")
    @Authorization
    public ResponseEntity<BaseResult> remove(@RequestBody List<String> ids) {
        UpmsCategoryDictExample upmsCategoryDictExample = new UpmsCategoryDictExample();
        upmsCategoryDictExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(upmsCategoryDictExample));
    }
}
